﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using fideeraSoft.Utils;
using System.Windows.Forms;
using fideeraSoft.Entities;
using fideeraSoft.Forms;

namespace fideeraSoft.Paneles
{
    public class ProduccionPanelInicio:PanelGeneral
    {
        private System.Windows.Forms.Button btAgregarMateriaPrima;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btProducto;
        private System.Windows.Forms.ListBox lsbMateriaPrima;
        private System.Windows.Forms.TableLayoutPanel tlpGral;
        private System.Windows.Forms.TableLayoutPanel tlpListaMateriaPrima;
        private System.Windows.Forms.DateTimePicker dtFechaInicio;
        private System.Windows.Forms.Label lbProductoNombre;
        private System.Windows.Forms.Button btRemoverMateriaPrima;
        private System.Windows.Forms.GroupBox gbGral;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.Label lbId;
        private System.Windows.Forms.Button btSubmit;
        private System.Windows.Forms.Label lbFechaInicio;
        private System.Windows.Forms.Label lbObservacion;
        private System.Windows.Forms.TextBox tbObservacion;
        private System.Windows.Forms.Label lbIdProduccion;
        private Producto producto;
        private int posicionTablaBotones = 0;
        private Produccion produccion;


        public ProduccionPanelInicio(Handler handler)
        {

            produccion=new Produccion();
            
           // produccion.LstMatPrima= new List<MateriaPrima>();

            this.formatearPanelGeneral(handler);

            this.InitializeComponent();
            this.formatearTableTitulo("INICIO DE PRODUCCIÓN");


            this.formatearTablaListaLote();
            this.formatearGroupBoxGral();
            this.formatearTablaGral();
            
            formatearTablaBotones();

            this.Controls.Add(gbGral);

            this.Controls.Add(tlpBotones);

        }


        #region FORMATEO
        private void formatearGroupBoxGral()
        {


            gbGral.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, gbGral.Size.Width), this.InitialRealPointVertical);

            gbGral.Controls.Add(tlpGral);
        }

        private void formatearTablaGral()
        {

            tlpGral.ColumnStyles.Clear();
            for (int i = 0; i < tlpGral.ColumnCount; i++)
            {
                tlpGral.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            }

            tlpGral.RowStyles.Clear();
            for (int i = 0; i < tlpGral.RowCount; i++)
            {
                tlpGral.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }
                                              

            tlpGral.Location = new Point(MetodosGenerales.calcularCenterWidth(gbGral.Size.Width, tlpGral.Size.Width), 30);



            cargarControlesEnTabla();

            
        }

        private void cargarControlesEnTabla()
        {
            tlpGral.Controls.Add(lbId);

            lbIdProduccion.Text = this.handler.buscarNuevoUltimoIdProduccionBD().ToString();
            tlpGral.Controls.Add(lbIdProduccion);
            

            tlpGral.Controls.Add(btProducto);            
            tlpGral.Controls.Add(lbProductoNombre);


            tlpGral.Controls.Add(lbFechaInicio);
            dtFechaInicio.Value = System.DateTime.Now;
            tlpGral.Controls.Add(dtFechaInicio);
                        

            tlpGral.Controls.Add(lbObservacion);            
            tlpGral.Controls.Add(tbObservacion);


            tlpGral.Controls.Add(btAgregarMateriaPrima);
            //tlpGral.Controls.Add(lsbLote);
            tlpGral.Controls.Add(tlpListaMateriaPrima);

        }
        private void formatearTablaListaLote()
        {
            tlpListaMateriaPrima.Controls.Add(lsbMateriaPrima);
            tlpListaMateriaPrima.Controls.Add(btRemoverMateriaPrima);
        }
        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btSubmit);

            posicionTablaBotones = InitialRealPointVertical + gbGral.Size.Height + 40;
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionTablaBotones);

        }
        #endregion

        #region INICIALIZAR COMPONETES
        private void InitializeComponent()
        {
            this.btAgregarMateriaPrima = new System.Windows.Forms.Button();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btProducto = new System.Windows.Forms.Button();
            this.lsbMateriaPrima = new System.Windows.Forms.ListBox();
            this.tlpGral = new System.Windows.Forms.TableLayoutPanel();
            this.tlpListaMateriaPrima = new System.Windows.Forms.TableLayoutPanel();
            this.dtFechaInicio = new System.Windows.Forms.DateTimePicker();
            this.lbProductoNombre = new System.Windows.Forms.Label();
            this.btRemoverMateriaPrima = new System.Windows.Forms.Button();
            this.gbGral = new System.Windows.Forms.GroupBox();
            this.btVolver = new System.Windows.Forms.Button();
            this.lbId = new System.Windows.Forms.Label();
            this.btSubmit = new System.Windows.Forms.Button();
            this.lbFechaInicio = new System.Windows.Forms.Label();
            this.lbIdProduccion = new System.Windows.Forms.Label();
            this.lbObservacion = new System.Windows.Forms.Label();
            this.tbObservacion = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btAgregarMateriaPrima
            // 
            this.btAgregarMateriaPrima.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.btAgregarMateriaPrima.BackColor = System.Drawing.Color.AliceBlue;
            this.btAgregarMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btAgregarMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.btAgregarMateriaPrima.Name = "btAgregarMateriaPrima";
            this.btAgregarMateriaPrima.Size = new System.Drawing.Size(100, 50);
            this.btAgregarMateriaPrima.TabIndex = 0;
            this.btAgregarMateriaPrima.Text = "Agregar Materia Prima / Insumo";
            this.btAgregarMateriaPrima.UseVisualStyleBackColor = false;
            this.btAgregarMateriaPrima.Click += new System.EventHandler(this.btAgregarMateriaPrima_Click);
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSize = true;
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 100);
            this.tlpBotones.TabIndex = 0;
            // 
            // btProducto
            // 
            this.btProducto.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btProducto.BackColor = System.Drawing.Color.AliceBlue;
            this.btProducto.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btProducto.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btProducto.Location = new System.Drawing.Point(0, 0);
            this.btProducto.Name = "btProducto";
            this.btProducto.Size = new System.Drawing.Size(75, 23);
            this.btProducto.TabIndex = 0;
            this.btProducto.Text = "Producto";
            this.btProducto.UseVisualStyleBackColor = false;
            this.btProducto.Click += new System.EventHandler(this.btProducto_Click);
            // 
            // lsbMateriaPrima
            // 
            this.lsbMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lsbMateriaPrima.FormattingEnabled = true;
            this.lsbMateriaPrima.ItemHeight = 18;
            this.lsbMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.lsbMateriaPrima.Name = "lsbMateriaPrima";
            this.lsbMateriaPrima.Size = new System.Drawing.Size(280, 238);
            this.lsbMateriaPrima.TabIndex = 0;
            // 
            // tlpGral
            // 
            this.tlpGral.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpGral.BackColor = System.Drawing.Color.LightCyan;
            this.tlpGral.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpGral.ColumnCount = 2;
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.Location = new System.Drawing.Point(0, 0);
            this.tlpGral.Name = "tlpGral";
            this.tlpGral.RowCount = 7;
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.tlpGral.Size = new System.Drawing.Size(700, 450);
            this.tlpGral.TabIndex = 0;
            // 
            // tlpListaMateriaPrima
            // 
            this.tlpListaMateriaPrima.ColumnCount = 2;
            this.tlpListaMateriaPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 70F));
            this.tlpListaMateriaPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
            this.tlpListaMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.tlpListaMateriaPrima.Name = "tlpListaMateriaPrima";
            this.tlpListaMateriaPrima.RowCount = 1;
            this.tlpListaMateriaPrima.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpListaMateriaPrima.Size = new System.Drawing.Size(400, 300);
            this.tlpListaMateriaPrima.TabIndex = 0;
            // 
            // dtFechaInicio
            // 
            this.dtFechaInicio.Location = new System.Drawing.Point(0, 0);
            this.dtFechaInicio.Name = "dtFechaInicio";
            this.dtFechaInicio.Size = new System.Drawing.Size(350, 20);
            this.dtFechaInicio.TabIndex = 0;
            // 
            // lbProductoNombre
            // 
            this.lbProductoNombre.AutoSize = true;
            this.lbProductoNombre.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbProductoNombre.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbProductoNombre.Location = new System.Drawing.Point(0, 0);
            this.lbProductoNombre.Name = "lbProductoNombre";
            this.lbProductoNombre.Size = new System.Drawing.Size(100, 23);
            this.lbProductoNombre.TabIndex = 0;
            this.lbProductoNombre.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // btRemoverMateriaPrima
            // 
            this.btRemoverMateriaPrima.BackColor = System.Drawing.Color.AliceBlue;
            this.btRemoverMateriaPrima.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btRemoverMateriaPrima.Location = new System.Drawing.Point(0, 0);
            this.btRemoverMateriaPrima.Name = "btRemoverMateriaPrima";
            this.btRemoverMateriaPrima.Size = new System.Drawing.Size(120, 50);
            this.btRemoverMateriaPrima.TabIndex = 0;
            this.btRemoverMateriaPrima.Text = "Remover Materia Prima";
            this.btRemoverMateriaPrima.UseVisualStyleBackColor = false;
            this.btRemoverMateriaPrima.Click += new System.EventHandler(this.btRemoverMateriaPrima_Click);
            // 
            // gbGral
            // 
            this.gbGral.Font = new System.Drawing.Font("Bodoni MT", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbGral.Location = new System.Drawing.Point(0, 0);
            this.gbGral.Name = "gbGral";
            this.gbGral.Size = new System.Drawing.Size(800, 550);
            this.gbGral.TabIndex = 0;
            this.gbGral.TabStop = false;
            this.gbGral.Text = "Orden de Producción";
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // lbId
            // 
            this.lbId.AutoSize = true;
            this.lbId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbId.Location = new System.Drawing.Point(0, 0);
            this.lbId.Name = "lbId";
            this.lbId.Size = new System.Drawing.Size(100, 23);
            this.lbId.TabIndex = 0;
            this.lbId.Text = "ID / Número de Partida";
            this.lbId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // btSubmit
            // 
            this.btSubmit.BackColor = System.Drawing.Color.AliceBlue;
            this.btSubmit.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btSubmit.ForeColor = System.Drawing.Color.Black;
            this.btSubmit.Location = new System.Drawing.Point(0, 0);
            this.btSubmit.Name = "btSubmit";
            this.btSubmit.Size = new System.Drawing.Size(75, 40);
            this.btSubmit.TabIndex = 0;
            this.btSubmit.Text = "Iniciar";
            this.btSubmit.UseVisualStyleBackColor = false;
            this.btSubmit.Click += new System.EventHandler(this.btSubmit_Click);
            // 
            // lbFechaInicio
            // 
            this.lbFechaInicio.AutoSize = true;
            this.lbFechaInicio.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbFechaInicio.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbFechaInicio.Location = new System.Drawing.Point(0, 0);
            this.lbFechaInicio.Name = "lbFechaInicio";
            this.lbFechaInicio.Size = new System.Drawing.Size(100, 23);
            this.lbFechaInicio.TabIndex = 0;
            this.lbFechaInicio.Text = "Fecha Inicio";
            this.lbFechaInicio.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lbIdProduccion
            // 
            this.lbIdProduccion.AutoSize = true;
            this.lbIdProduccion.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbIdProduccion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbIdProduccion.Location = new System.Drawing.Point(0, 0);
            this.lbIdProduccion.Name = "lbIdProduccion";
            this.lbIdProduccion.Size = new System.Drawing.Size(100, 23);
            this.lbIdProduccion.TabIndex = 0;
            this.lbIdProduccion.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lbObservacion
            // 
            this.lbObservacion.AutoSize = true;
            this.lbObservacion.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbObservacion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbObservacion.Location = new System.Drawing.Point(0, 0);
            this.lbObservacion.Name = "lbObservacion";
            this.lbObservacion.Size = new System.Drawing.Size(100, 23);
            this.lbObservacion.TabIndex = 0;
            this.lbObservacion.Text = "Observaciones";
            this.lbObservacion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbObservacion
            // 
            this.tbObservacion.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbObservacion.Location = new System.Drawing.Point(0, 0);
            this.tbObservacion.MaxLength = 250;
            this.tbObservacion.Name = "tbObservacion";
            this.tbObservacion.Size = new System.Drawing.Size(400, 30);
            this.tbObservacion.TabIndex = 0;
            this.ResumeLayout(false);

        }
        #endregion

        private void btProducto_Click(object sender, EventArgs e)
        {
            producto = new Producto();

            new FormAddProductoAProduccion(producto,lbProductoNombre);
        }

        private void btAgregarMateriaPrima_Click(object sender, EventArgs e)
        {
            if (lsbMateriaPrima.Items.Count < 13)
            {
                new FormAddMateriaPrimaAProduccion(lsbMateriaPrima, produccion.LstMatPrima);
            }
            else
            {
                MessageBox.Show("Máximo de materias primas permitidas.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void btRemoverMateriaPrima_Click(object sender, EventArgs e)
        {
            try
            {
                 if (lsbMateriaPrima.Items.Count <= 0)
                {
                    MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    if((lsbMateriaPrima.SelectedIndex != null)&&(lsbMateriaPrima.SelectedItem!=null))
                    {
                        string strItem = lsbMateriaPrima.Items[lsbMateriaPrima.SelectedIndex].ToString();

                    
                        int inStart=strItem.LastIndexOf(" - ")+3;
                        int inLenght=strItem.LastIndexOf(" · ")-inStart;
                        string strCantidad= strItem.Substring(inStart,inLenght);
                        float flCantidad= (float)Convert.ToDouble(strCantidad.Trim());

                        inStart=strItem.IndexOf(": ") + 2;
                        inLenght=strItem.IndexOf(" - ")-inStart;
                        string strID= strItem.Substring(inStart,inLenght);
                        int inID = Convert.ToInt32(strID);

                    
                       // MessageBox.Show("INDEX: " + start + " - LENGH: " + lenght + " - ITEM: " + item.Substring(start, lenght), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        foreach (MateriaPrima matPrim in produccion.LstMatPrima)
                        {
                            if (matPrim.IdMateriaPrima == inID)
                            {
                                produccion.LstMatPrima.Remove(matPrim);
                                break;
                            }

                        }

                        lsbMateriaPrima.Items.Remove(lsbMateriaPrima.SelectedItem);
                    }
                    else 
                    {
                        MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA SELECCIONADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                
                }
            }
            catch (Exception exc)
            {

                CLogger.Append("[btRemoverMateriaPrima_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }
    
        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btSubmit_Click(object sender, EventArgs e)
        {

            try
            {
                if (validarCampos())
                {
                    MessageBox.Show("DATOS INCOMPLETOS. Asegurese de completar todos los datos necesarios.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                
                produccion.IdProduccion = Convert.ToInt32(lbIdProduccion.Text);
                produccion.FechaInicio = dtFechaInicio.Value.Date;
                produccion.Producto = producto.IdProducto;
                if (string.IsNullOrWhiteSpace(tbObservacion.Text)) produccion.Observacion = ""; else produccion.Observacion = tbObservacion.Text;
                
                DialogResult res = MessageBox.Show("Desea comenzar con la producción:\n ID: "+produccion.IdProduccion+" \n Producto: "+producto.Nombre+" \n Fecha de Inicio: "+produccion.FechaInicio.Date.ToString("d")+" \n Observación: "+produccion.Observacion , "INFO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.No)
                {
                    return;
                }
                
                handler.altaProduccion(produccion);


                Produccion_X_MateriaPrima prodXMatPrim = new Produccion_X_MateriaPrima();

                prodXMatPrim.Id= handler.buscarNuevoUltimoIdProduccionXMatPrimaBD();
                prodXMatPrim.Produccion = produccion.IdProduccion;

                foreach(MateriaPrima matPrima in produccion.LstMatPrima)
                {

                    prodXMatPrim.MateriaPrima = matPrima.IdMateriaPrima;
                    prodXMatPrim.Cantidad = matPrima.Cantidad;
                    prodXMatPrim.IdLote = matPrima.IdLote;
                    prodXMatPrim.Unidad = matPrima.Unidad;
                    handler.altaProduccionXMatPrima(prodXMatPrim);
                    
                    prodXMatPrim.Id++;

                    MateriaPrimaXLotes matPrimXLot= handler.buscarMateriaPrimaXLotePorIdMatYLote(matPrima);
                    matPrimXLot.Cantidad = matPrimXLot.Cantidad - matPrima.Cantidad;
                    handler.updateCantidadesMateriaPrimaXLote(matPrimXLot);
                    
                }

                MessageBox.Show("Producción comenzada correctamente.", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                limpiarTextBoxs();
            }
            catch (Exception exc)
            {

                CLogger.Append("[btSubmit_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }

        }

        private void limpiarTextBoxs()
        {
            tbObservacion.Clear();

            lbIdProduccion.Text = "";
            lbIdProduccion.Text = handler.buscarNuevoUltimoIdProduccionBD().ToString();
            
            lbProductoNombre.Text = "";

            produccion = new Produccion();
            producto = new Producto();

            dtFechaInicio.Value = System.DateTime.Now;

            lsbMateriaPrima.Items.Clear();
            
        }

        private bool validarCampos()
        {
            return (lbIdProduccion == null) ||(producto.IdProducto==null) || (string.IsNullOrWhiteSpace(lbProductoNombre.Text)) || (lsbMateriaPrima.Items.Count <= 0) ||(produccion.LstMatPrima.Count<=0) || (dtFechaInicio.Value == null);
        }
    }
}
